package com.easipass.framework.eirframeworkcoredsl.graph;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(Include.NON_EMPTY)
public class Link {

    public final static String PROPERTY_TRANSITION_NAME = "transitionName";

    public String from;

    public String to;

    public String id;

    public Map<String, String> properties = null;

    Link() {

    }

    public Link(String sourceId, String targetId) {
        this.from = sourceId;
        this.to = targetId;
        this.id = generatedId();
    }

    public Link(String sourceId, String targetId, String transitionName) {
        this.from = sourceId;
        this.to = targetId;
        properties = new HashMap<>();
        properties.put(PROPERTY_TRANSITION_NAME, transitionName);
        this.id = generatedId();
    }

    @Override
    public String toString() {
        StringBuilder s = new StringBuilder();
        s.append("Link[from=").append(from).append(",to=").append(to);
        if (properties != null) {
            s.append(",properties=").append(properties);
        }
        s.append("]");
        return s.toString();
    }

    public String generatedId(){
        return "link-" + UUID.randomUUID();
    }

    public boolean hasTransitionSet() {
        return properties != null && properties.containsKey(PROPERTY_TRANSITION_NAME);
    }

    @JsonIgnore
    public String getTransitionName() {
        return properties != null ? properties.get(PROPERTY_TRANSITION_NAME) : null;
    }

    public void updateFrom(String newFrom) {
        this.from = newFrom;
    }

    public void updateTo(String newTo) {
        this.to = newTo;
    }
}
